package DatabaseAccessing;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class SessionFactoryUtil {

    private static SessionFactory sessionFactory;
    private static Session session = null;

    private SessionFactoryUtil() {
    }

    public static Session getInstance() {
        if (session == null) {
            session = sessionFactory.openSession();
        }
        return session;
    }

    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Exception exp) {
            System.err.println("Error building the session factory!"
                    + " (SessionFactoryUtil - Floating code block)"
                    + exp.getMessage());
        }
    }

    public static void close() {
        if (sessionFactory != null) {
            sessionFactory.close();
        }
        sessionFactory = null;

    }
}
